package com.guotd.jspwork2.controller;

import com.guotd.jspwork2.utils.JDBCUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import static com.guotd.jspwork2.utils.JDBCUtil.getConnection;

@WebServlet("/delTV")
public class delTVServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String tvId = request.getParameter("tvId");
        String kind=request.getParameter("kind");
        /*try {
            Connection con = getConnection();
            PreparedStatement pstmt = con.prepareStatement("delete from tv where id=?");
            pstmt.setString(1, tvId);
            request.setAttribute("kind_msg", "删除成功");
            int row = pstmt.executeUpdate();
            if (row > 0) {
                request.getSession().setAttribute("tv_msg", "删除成功");
                request.getSession().setAttribute("kind",kind);
                request.getRequestDispatcher("getAllTVs").forward(request, response);
            } else {
                request.getSession().setAttribute("tv_msg", "删除失败，商品不存在！");
                request.getRequestDispatcher("getAllTVs").forward(request, response);
            }
        } catch (SQLException e) {
            request.getSession().setAttribute("tv_msg", "删除失败！");
            request.getRequestDispatcher("getAllTVs").forward(request, response);
        }*/
        String sql="delete from tv where id=?";
        String []args={tvId};
        int row= JDBCUtil.update(sql, args);
        if (row > 0) {
            request.getSession().setAttribute("tv_msg", "删除成功");
            request.getSession().setAttribute("kind",kind);
            request.getRequestDispatcher("getAllTVs").forward(request, response);
        } else {
            request.getSession().setAttribute("tv_msg", "删除失败，商品不存在！");
            request.getRequestDispatcher("getAllTVs").forward(request, response);
        }
    }
}
